package com.chenxingnet.controller.back;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.chenxingnet.dto.Role;
import com.chenxingnet.dto.UserDepartment;
import com.chenxingnet.dto.Usercontrol;
import com.chenxingnet.service.ManagerService;
import com.chenxingnet.service.UserDeptService;
import com.chenxingnet.tools.MD5Tools;

@Controller
@RequestMapping("/back")
public class BackController {
	
	@Resource
	ManagerService managerService;
	@Resource
	private UserDeptService userDeptService;
	
	
	/**
	 * 用户登录
	 */
	@RequestMapping("/userLogin")
	public String userLogin(HttpServletRequest request,HttpSession session,Model model,HttpServletResponse response,RedirectAttributes  attr){
		String  uAccount=request.getParameter("username");
		String  uPassword=request.getParameter("pwd");
		Role role = null;
		UserDepartment userDept = null;
		//调用service层userLogin方法，根据账号和密码判断登录信息是否正确
		Usercontrol user = managerService.userLogin(uAccount,MD5Tools.MD5(uPassword) );
		if(user==null){
			model.addAttribute("result", "登录失败，用户名或者密码错误。");
			return "index";
		}else{
			 //记住用户名、密码功能
	         String remFlag = request.getParameter("remFlag");
	         if("1".equals(remFlag)){ //"1"表示用户勾选记住密码
	             String loginInfo = uAccount+","+uPassword;
	             Cookie userCookie=new Cookie("loginInfo",loginInfo); 
	             userCookie.setMaxAge(7*24*60*60);   //存活期为七天 7*24*60*60
	             userCookie.setPath("/");
	             response.addCookie(userCookie); 
	         }
	         
			 session.setAttribute("user", user);
			 role = managerService.selectByRid(user.getRoleid());
			 session.setAttribute("role", role);
			 userDept = userDeptService.selectByUid(user.getUid());
			 
			if(!uAccount.equals(user.getUaccount()) && !uPassword.equals(user.getUpassword())){
				model.addAttribute("result", "对不起，您没有权限登录后台管理系统。");
				return "index";
			}
			
			//会计
			if(role.getRoleid()==4){
				model.addAttribute("role", role);
				model.addAttribute("user", user);
				return "redirect:/accountlist/selectAccountList?pageNow=1&deptname=&zhiling=&chexing=&xingming=&jiesuan=&jiashiyuanjs=&fatime1=&fatime2=";
			 }
			//role.getRoleid()==2&&userdept.getDeptId==1  成都部门经理
			if(role.getRoleid()==2 && userDept.getDeptid()==1){
				model.addAttribute("role", role);
				model.addAttribute("user", user);
				attr.addAttribute("rolename",role.getRolename());
				return "redirect:/department/chengCity?deptid=1";
			 }
			//role.getRoleid()==2&&userdept.getDeptId==2  绵阳部门经理
			if(role.getRoleid()==2 && userDept.getDeptid()==2){
				model.addAttribute("role", role);
				model.addAttribute("user", user);
				attr.addAttribute("rolename",role.getRolename());
				return "redirect:/department/mianCity?deptid=2";
			 }
			//role.getRoleid()==2&&userdept.getDeptId==4  绵专部门经理
			if(role.getRoleid()==2 && userDept.getDeptid()==4){
				model.addAttribute("role", role);
				model.addAttribute("user", user);
				attr.addAttribute("rolename",role.getRolename());
				return "redirect:/department/mzCity?deptid=4";
			}
			//成都的业务员
			if(role.getRoleid()==3 && userDept.getDeptid()==1){
				model.addAttribute("role", role);
				model.addAttribute("user", user);
				attr.addAttribute("rolename",role.getRolename());
				return "redirect:/department/chengCity?deptid=1";
			 }
			//绵阳的业务员
			if(role.getRoleid()==3 && userDept.getDeptid()==2){
				model.addAttribute("role", role);
				model.addAttribute("user", user);
				attr.addAttribute("rolename",role.getRolename());
				return "redirect:/department/mianCity?deptid=2";
			 }
			//绵专的业务员
			if(role.getRoleid()==3 && userDept.getDeptid()==4){
				model.addAttribute("role", role);
				model.addAttribute("user", user);
				attr.addAttribute("rolename",role.getRolename());
				return "redirect:/department/mzCity?deptid=4";
			}
			
		}
		model.addAttribute("role", role);
		model.addAttribute("user", user);
		return "redirect:/back/guanli";
	}
	/**
	 * 用户退出登录
	 */
	@RequestMapping("/userLoginOut")
	public String userLoginOut(HttpSession session){
		 
		session.invalidate();
		//返回到登录页面
		return "index";
		
	}
	/**
	 * 转至管理员页面
	 * @return
	 */
	@RequestMapping("/guanli")
	public String manger(HttpServletRequest request,Model model){
		try {
			String rolename = new String(request.getParameter("rolename").getBytes("ISO-8859-1"),"UTF-8");
			if(rolename.equals("会计")){
				model.addAttribute("result", "对不起，您无此操作权限");
				return "account_list";
			}
			Integer deptid = Integer.valueOf(request.getParameter("deptid"));
			
			if(deptid != null){
				model.addAttribute("deptid", deptid);
			}
			if(deptid == 2 && (rolename.equals("业务员"))){
				model.addAttribute("result", "对不起，您无此操作权限");
				model.addAttribute("deptid", deptid);
				return "aaa_city";
			}
			if(deptid == 4 && (rolename.equals("业务员"))){
				model.addAttribute("result", "对不起，您无此操作权限");
				model.addAttribute("deptid", deptid);
				return "mz_city";
			}
			if(deptid == 1 && (rolename.equals("业务员"))){
				model.addAttribute("result", "对不起，您无此操作权限");
				model.addAttribute("deptid", deptid);
				return "city";
			}
		} catch (Exception e) {
			
		}
		return "guanli";
	}
	
	

}
